﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SampleAppAPI;
using SampleApp.Infrastructure;
using SampleApp.RandomDataGenerators;
using System.Windows.Threading;

namespace SampleApp
{
    public class OrdersViewModel
    {
        public ObservableCollection<IOrder> Orders { get; protected set; }

        private OMSOrderDataGenerator omsOrderDataGenerator;
        private readonly Dispatcher _dispatcher;

        public OrdersViewModel()
        {
            _dispatcher = Dispatcher.CurrentDispatcher;
            Orders = new ObservableCollection<IOrder>();
            omsOrderDataGenerator = new OMSOrderDataGenerator(100);
            omsOrderDataGenerator.NewOMSOrderData += new NewOMSOrderDataHandler(omsOrderDataGenerator_NewOMSOrderData);
            //for (int i = 0; i < 1000; i++)
            //{
            //    IDataRow orderData = new OMSOrderData() { OrderID = Guid.NewGuid(), LimitPrice = 10 + i, Symbol = "MSFT", CompanyName = "Microsoft Corporation" };
            //    Order order = new Order(new List<IDataRow>() { orderData });
            //    Orders.Add(order);
            //}
        }

        void omsOrderDataGenerator_NewOMSOrderData(object sender, OMSOrderDataEventArgs e)
        {
            
            IDataRow orderData = new OMSOrderData
            {
                OrderID = e.OrderData.OrderID,
                LimitPrice = e.OrderData.LimitPrice,
                CompanyName = e.OrderData.CompanyName,
                Symbol = e.OrderData.Symbol
            };

            _dispatcher.BeginInvoke(new Action<IDataRow>(newData => Orders.Add(new Order(new List<IDataRow> { newData}))), orderData);
        }
    }
}
